<template>
  <div class="app-container">
    <div class="menu-container">
      <el-form :inline="true" style="float: left;">
        <el-button size="mini" type="primary" @click="createElement()">添加元素</el-button>
      </el-form>
      <div style="clear:both"/>
    </div>
    <el-table v-loading="listLoading" :data="page.records" border fit highlight-current-row style="width: 100%" height="500">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="名称">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="图标">
        <template slot-scope="scope">
          <img :src="scope.row.url" style="height:60px" >
        </template>
      </el-table-column>

      <el-table-column width="120px" label="单位">
        <template slot-scope="scope">
          <span>{{ scope.row.unit }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" label="佣金价格">
        <template slot-scope="scope">
          <span>{{ scope.row.price }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" label="类型">
        <template slot-scope="scope">
          <span>{{ scope.row.type_desc }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" label="等级">
        <template slot-scope="scope">
          <span>{{ scope.row.level }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Actions" width="250">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="modifyElement(scope.row)">修改</el-button>
          <!-- <el-button type="danger" size="small" icon="el-icon-delete" @click="delCategory(scope.row)">删除</el-button>
          <el-button type="primary" size="small" @click="managerCategoryOptions(scope.row)">分类选项管理</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="page.recordsTotal>0" :total="page.recordsTotal" :page.sync="page.pageNum" :page-sizes="[20, 50, 100]" :limit.sync="page.pageSize" @pagination="getList" />

    <el-dialog :visible.sync="dialogFormVisible" :title="dialogTitle" log- width="80%">
      <element-detail :is-edit="isEdit" :current-id="currentElementId" @successElementOperate="successElementOperate"/>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList } from '@/api/elements'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import ElementDetail from './components/ElementDetail'

export default {
  name: 'ElementsList',
  components: { Pagination, ElementDetail },
  data() {
    return {
      dialogTitle: '新增分类',
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      isEdit: false,
      currentElementId: undefined,
      formLabelWidth: '100px',
      dialogFormVisible: false,
      stateOptions: [{ value: 0, label: '正常' }, { value: 1, label: '禁用' }],
      categoryForm: { 'id': undefined, 'name': '', 'show_order': 1, 'state': 0 },
      page: { pageNum: 1, pageSize: 100, pageTotal: 0, recordsTotal: 0, records: [] }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    createElement() {
      this.isEdit = false
      this.currentElementId = undefined
      this.dialogTitle = '添加视频元素'
      this.dialogFormVisible = true
    },
    modifyElement(element) {
      this.isEdit = true
      this.currentElementId = element.id
      this.dialogTitle = '编辑视频元素'
      this.dialogFormVisible = true
    },
    successElementOperate() {
      this.getList()
      this.isEdit = false
      this.dialogFormVisible = false
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        const data = response.data
        if (data.code !== 0) {
          this.$message.error(data.msg)
        }
        this.page = data.data
        this.listLoading = false
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
